class User {
  final int? id;
  final String username;
  final String email;
  final String? displayName;
  final String? avatar;
  final String? partnerName;
  final DateTime? relationshipStartDate;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  User({
    this.id,
    required this.username,
    required this.email,
    this.displayName,
    this.avatar,
    this.partnerName,
    this.relationshipStartDate,
    this.createdAt,
    this.updatedAt,
  });

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'],
      username: json['username'],
      email: json['email'],
      displayName: json['displayName'],
      avatar: json['avatar'],
      partnerName: json['partnerName'],
      relationshipStartDate: json['relationshipStartDate'] != null
          ? DateTime.parse(json['relationshipStartDate'])
          : null,
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'])
          : null,
      updatedAt: json['updatedAt'] != null
          ? DateTime.parse(json['updatedAt'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      if (id != null) 'id': id,
      'username': username,
      'email': email,
      if (displayName != null) 'displayName': displayName,
      if (avatar != null) 'avatar': avatar,
      if (partnerName != null) 'partnerName': partnerName,
      if (relationshipStartDate != null)
        'relationshipStartDate': relationshipStartDate!.toIso8601String(),
      if (createdAt != null) 'createdAt': createdAt!.toIso8601String(),
      if (updatedAt != null) 'updatedAt': updatedAt!.toIso8601String(),
    };
  }

  User copyWith({
    int? id,
    String? username,
    String? email,
    String? displayName,
    String? avatar,
    String? partnerName,
    DateTime? relationshipStartDate,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return User(
      id: id ?? this.id,
      username: username ?? this.username,
      email: email ?? this.email,
      displayName: displayName ?? this.displayName,
      avatar: avatar ?? this.avatar,
      partnerName: partnerName ?? this.partnerName,
      relationshipStartDate: relationshipStartDate ?? this.relationshipStartDate,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  String toString() {
    return 'User{id: $id, username: $username, email: $email, displayName: $displayName}';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is User &&
          runtimeType == other.runtimeType &&
          id == other.id &&
          username == other.username &&
          email == other.email;

  @override
  int get hashCode => id.hashCode ^ username.hashCode ^ email.hashCode;
}